// 指令

// 获取焦点
export const focus = {
    inserted: function (el) {
        el.focus()
    }
}

// 选项上拉加载
export const selectLoadMore = {
    // 只调用一次，指令第一次绑定到元素时调用
    bind(el, binding) {
        const SELECTDOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
        SELECTDOM.addEventListener('scroll', function () {
            const condition = this.scrollHeight - this.scrollTop == this.clientHeight
            if (condition) binding.value()
        })
    },
    // 当被绑定的元素插入到 DOM 中时
    inserted(el, binding) {
        binding.value()
    }
}

// 按钮权限
export const hasPerm = {
    inserted(el, binding, vnode) {
        // 按钮绑定的值 - 按钮类型
        const { value } = binding
        // 	函数里面第三个参数vnode 它的vnode.context就是当前的vm实例
        let that = vnode.context
        let permList = that.$route.meta.permList || []
        if (value) {
            const hasPermission = permList.some(permission => {
                return permission === value
            })
            if (!hasPermission) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        } else {
            throw new Error(`请设置操作权限标签值`)
        }
    }
}
